package com.lynchcms.cms.dao.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.stereotype.Repository;

import com.lynchcms.cms.dao.CmsAdminDao;
import com.lynchcms.cms.model.entity.CmsAdmin;
import com.lynchcms.cms.model.entity.CmsAdminRole;
import com.lynchcms.cms.model.entity.CmsAuthoritiy;
import com.lynchcms.cms.model.entity.CmsRole;
import com.lynchcms.cms.model.entity.CmsRoleAuthoritiy;
import com.lynchcms.core.dao.impl.BaseDaoImpl;

/**
 * 管理员业务处理DAO--Impl
 * 
 * @author Lynch
 */
@Repository("cmsAdminDao")
public class CmsAdminDaoImpl extends BaseDaoImpl<CmsAdmin, Long> implements
		CmsAdminDao {

	/**
	 * 根据用户名字查询用户 
	 */
	public CmsAdmin getByName(String userName) {
		
		Session session = this.getSession();
		String hql = "FROM CmsAdmin AS ca WHERE ca.adminName = ?";
		Query query = session.createQuery(hql);
		query.setString(0, userName);
		List<CmsAdmin> list = query.list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}else {
			return null;
		}
	}
	
	
	
//	/**
//	 * 根据用户名权限信息组装成框架需要的权限
//	 * 
//	 * @return 权限信息
//	 */
//	public List<GrantedAuthority> getAuthByUserName(CmsAdmin CmsAdmin) {
//
//		try {
//			List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
//			Set<CmsAuthoritiy> authorities = loadUserAuthorities(CmsAdmin);
//			Iterator<CmsAuthoritiy> iterator = authorities.iterator();
//			while (iterator.hasNext()) {
//				CmsAuthoritiy cmsAuthoritiy = iterator.next();
//				GrantedAuthorityImpl auth = new GrantedAuthorityImpl(cmsAuthoritiy.getAuthoritiyName());
//				auths.add(auth);
//			}
//			return auths;
//		} catch (RuntimeException re) {
//			return null;
//		}
//	}
	


}
